package com.itheima.easy.service.impl;

import com.itheima.easy.entity.DeptPostUser;
import com.itheima.easy.mapper.DeptMapper;
import com.itheima.easy.mapper.DeptPostUserMapper;
import com.itheima.easy.service.DeptPostUserService;
import com.itheima.easy.utils.BeanConv;
import com.itheima.easy.utils.EmptyUtil;
import com.itheima.easy.vo.DeptPostUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description：部门岗位用户关联表服务实现类
 */
@Service
public class DeptPostUserServiceImpl implements DeptPostUserService {

    @Autowired
    DeptMapper deptMapper;

    @Autowired
    DeptPostUserMapper deptPostUserMapper;

    @Override
    public List<DeptPostUserVo> findDeptPostUserVoListInUserId(List<Long> userIds) {
        List<DeptPostUser> deptPostUsers = deptPostUserMapper.findDeptPostUserListInUserId(userIds);
        return BeanConv.toBeanList(deptPostUsers,DeptPostUserVo.class);
    }

    @Override
    public Boolean deleteDeptPostUserByUserId(Long userId) {
        int flag = deptPostUserMapper.deleteDeptPostUserByUserId(userId);
        if (flag>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteDeptPostUserInUserId(List<Long> userIds) {
        int flag = deptPostUserMapper.deleteDeptPostUserInUserId(userIds);
        if (flag>0){
            return true;
        }
        return false;
    }

    @Override
    public DeptPostUserVo findDeptPostUserVoByUserId(Long userId) {
        List<DeptPostUser> deptPostUserList = deptPostUserMapper.findDeptPostUserByUserId(userId);
        if (!EmptyUtil.isNullOrEmpty(deptPostUserList)){
            DeptPostUserVo vo = new DeptPostUserVo();
            BeanUtils.copyProperties(deptPostUserList.get(0),vo);
            return vo;
        }
        return null;
    }

    @Override
    @Transactional
    public int batchInsert(List<DeptPostUser> deptPostUsers) {
        return deptPostUserMapper.batchInsert(deptPostUsers);
    }
}
